﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.TMS.DisableArea.DisableAreaRangeCoordinate;
using Dapper;

namespace BCData.TMS.DisableArea.DisableAreaRangeCoordinate
{
    public class DisableAreaRangeCoordinateData : IDisableAreaRangeCoordinateData
    {
        private readonly IDatabaseContext database;

        public DisableAreaRangeCoordinateData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<int> Add(long disableAreaId, List<DisableAreaRangeCoordinateEntity> disableAreaRangeCoordinateEntities)
        {
            int i = 0;
            foreach (var item in disableAreaRangeCoordinateEntities)
            {
                item.DisableAreaId = disableAreaId;
                //sqls.Add(GetInsertSql(item, out DynamicParameters parameter));
                string sql = GetInsertSql(item, out DynamicParameters parameter);
                //parameters.Add(parameter);
                i += await database.Master.ExecuteAsync(sql, parameter);
            }

            return i;
        }

        private string GetInsertSql(DisableAreaRangeCoordinateEntity entity, out DynamicParameters parameters)
        {
            parameters = new DynamicParameters();
            string sql = SqlCommands.GetInsertSql("DisableAreaRangeCoordinate", new string[] { "DisableAreaId", "Longitude", "Latitude", "CoordinateIndex" });
            parameters.Add("?DisableAreaId", entity.DisableAreaId, DbType.Int64, size: 20);
            parameters.Add("?Longitude", entity.Longitude, DbType.Double);
            parameters.Add("?Latitude", entity.Latitude, DbType.Double);
            parameters.Add("?CoordinateIndex", entity.CoordinateIndex, DbType.Int32, size: 10);
            return sql;
        }

        public bool Delete(long disableAreaId)
        {
            DynamicParameters parameters = new DynamicParameters();

            string DELETE_SQL = @"DELETE FROM DisableAreaRangeCoordinate WHERE DisableAreaId=?DisableAreaId";

            parameters.Add("?DisableAreaId", disableAreaId, DbType.Int64, size: 20);
            //return this.sqlCommand.Execute(DELETE_SQL, CommandType.Text, parameters) > 0;
            return database.Master.Execute(DELETE_SQL, parameters) > 0;
            //throw new NotImplementedException();
        }

        public async Task<IEnumerable<DisableAreaRangeCoordinateEntity>> QueryDisableAreaRangeCoordinate(long disableAreaId)
        {
            string QUERY = @"SELECT DisableAreaRangeCoordinateId,DisableAreaId, Longitude,Latitude,CoordinateIndex FROM DisableAreaRangeCoordinate ";

            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("DisableAreaId = ?DisableAreaId");
            parameters.Add("?DisableAreaId", disableAreaId, DbType.Int64);

            string sql = string.Format(@"{0} {1}", QUERY, wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "");

            //parameters.Add("?DisableAreaId", disableAreaId, DbType.Int64, size: 20);
            //return this.sqlCommand.Query<DisableAreaRangeCoordinateEntity>(sql, CommandType.Text, parameters);
            return await database.Slave.QueryAsync<DisableAreaRangeCoordinateEntity>(sql, parameters);
        }
    }
}
